<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/15
 * Time: 17:58
 */

namespace app\admin\model;


class PacketRecord extends DataBase
{
    protected $name = 'packet_record';
    protected $autoWriteTimestamp=true;

    public function getReceiveTimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }

    public function getDataList()
    {
        $page_config = array(
            'page' => input('get.offset',1),
            'list_rows' => input('get.limit',100)
        );
        $type = input('get.time');
        $start_time =strtotime(input('get.start_time'));
        $end_time = strtotime(input('get.end_time'));
        $key = input('get.key');
        $value = input('get.value');

        $where = [];
        if(!empty($type)){
            if(!empty($start_time) && !empty($end_time)){
                $where[] = [$type,'between',[$start_time,$end_time]];
            }
            if(!empty($start_time) && empty($end_time)){
                $where[] = [$type,'gt',$start_time];
            }
            if(empty($start_time) && !empty($end_time)){
                $where[] = [$type,'gt',$end_time];
            }
            if(!empty($key) && !empty($value)){

                $user_id = User::where('u_id','=',$value)->value('id');
                $where[] = [$key,'eq',$user_id];
            }

        }

        $data = $this->where($where)->paginate($page_config);

        foreach($data as $key => $value){
            $data[$key]['recive_user_id'] =  User::where('id','eq',$value['recive_user_id'])->value('u_id');
            $data[$key]['companion_id'] =  User::where('id','eq',$value['companion_id'])->value('u_id');
        }
        return $data;
    }
}